import { getDicList } from '@/request'
import { getSetting } from '@/settings'
import { getUrlQuery, setTitle } from '@/utils'
import vConsole from 'vconsole'
import type { App } from 'vue'
import { initSentry } from './initSentry'
import { initTracing } from './initTracing'
import { initRequest } from './initRequest'
import { useDictionariesStore } from '@/utils'
import { Lazyload } from 'vant'

export function initApp(app: App) {
  initTitle()
  initVConsole()
  initSentry()
  initTracing(app)
  initRequest()
  initVant(app)
}

function initVant(app: App) {
  app.use(Lazyload, {
    lazyComponent: true,
  })
}

function initTitle() {
  const { title = '' } = getSetting()

  setTitle(title)
}

function initVConsole() {
  const { vConsole: vconsole } = getUrlQuery()

  if (import.meta.env.VITE_ENV != 'prod' || vconsole == 'vconsole') {
    new vConsole()
  }
}

export function initDic() {
  return new Promise((resolve) => {
    getDicList({ dicType: 'FLS-COMMON-UI' })
      .then((res) => {
        useDictionariesStore().setData(res)
      })
      .finally(() => resolve('ok'))
  })
}
